package cn.outsourcing.supermarket.controller.backstage;

import cn.outsourcing.supermarket.common.Result;
import cn.outsourcing.supermarket.common.constants.CommonConstants;
import cn.outsourcing.supermarket.common.utils.QiNiuUtils;
import cn.outsourcing.supermarket.common.utils.ResultUtils;
import cn.outsourcing.supermarket.service.BackstageHomeService;
import cn.outsourcing.supermarket.service.HiolabsAdminService;
import cn.outsourcing.supermarket.vo.QiNiuUploadTokenVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author gzkemays
 * @since 2021/11/3 23:25
 */
@RestController
@RequestMapping("/admin/index")
public class IndexController {
  @Resource HiolabsAdminService adminService;
  @Resource BackstageHomeService backstageHomeService;

  @Value("${qiniu.url}")
  private String url;

  @Value("${qiniu.ak}")
  private String ak;

  @Value("${qiniu.sk}")
  private String sk;

  @GetMapping("/checkLogin")
  public Object getCheckLogin() {
    String loginStatus = adminService.checkLogin();
    if (loginStatus.equals("OK")) {
      return loginStatus;
    } else {
      return Result.builder().errmsg("请先登录").errno(401).build();
    }
  }

  @GetMapping("/main")
  public Result getMain(@RequestParam("pindex") Integer pindex) {
    return ResultUtils.simpleResult(backstageHomeService.getMainData(pindex));
  }

  @PostMapping("/getQiniuToken")
  public Result getQiNiuToken() {
    return ResultUtils.simpleResult(
        QiNiuUploadTokenVO.builder()
            .token(QiNiuUtils.getUploadToken(ak, sk, CommonConstants.QI_NIU_BUCKET))
            .url(url)
            .build());
  }
}
